package com.majorty.ucenter.service;

import com.majorty.framework.domain.ucenter.XcCompanyUser;
import com.majorty.framework.domain.ucenter.XcMenu;
import com.majorty.framework.domain.ucenter.XcUser;
import com.majorty.framework.domain.ucenter.ext.XcUserExt;
import com.majorty.ucenter.dao.XcCompanyUserRepository;
import com.majorty.ucenter.dao.XcMenuMapper;
import com.majorty.ucenter.dao.XcUserRepository;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户service
 *
 * @author zhangshuai
 * @date 2019-07-21 12:38
 */
@Service
public class UserService {

    @Autowired
    private XcUserRepository xcUserRepository;

    @Autowired
    private XcCompanyUserRepository xcCompanyUserRepository;

    @Autowired
    XcMenuMapper xcMenuMapper;

    //根据账号查询xcUser信息
    public XcUser findXcUserByUsername(String username) {
        return xcUserRepository.findByUsername(username);
    }

    public XcUserExt getUserExt(String username) {
        //根据账号信息查询用户基本信息
        XcUser xcUser = this.findXcUserByUsername(username);
        if (null == xcUser){
            return null;
        }
        //用户id
        String userId = xcUser.getId();
        //查询用户所有权限
        List<XcMenu> xcMenus = xcMenuMapper.selectPermissionByUserId(userId);

        //根据用户id查询用户所属公司id
        XcCompanyUser xcCompanyUser = xcCompanyUserRepository.findByUserId(userId);
        //取到用户的公司id
        String companyId = null;
        if(xcCompanyUser!=null){
            companyId = xcCompanyUser.getCompanyId();
        }
        XcUserExt xcUserExt = new XcUserExt();
        BeanUtils.copyProperties(xcUser,xcUserExt);
        xcUserExt.setCompanyId(companyId);
        //设置权限
        xcUserExt.setPermissions(xcMenus);
        return xcUserExt;
    }

}
